﻿using System;
using System.Collections.Generic;

namespace CoCoonCoreWM6
 {
    public class SmartCPU
     {
        //private short programCounter { get; set; }
	
        //private byte registerA { get; set; }
	
        //private byte registerB { get; set; }
	
        //private short registerD { get; set; }
	
        //private byte conditionCode { get; set; }
	
        //private short pointerX { get; set; }
	
        //private short pointerY { get; set; }
	
        //private short systemStack { get; set; }
	
        //private short userStack { get; set; }
	
        //private byte directPage { get; set; }

        private Dictionary<string, int> Registers;

        private Instruction[] InstructionSet;

        private string ProgramCounterKey;

        private string ConditionCodeKey;

        public SmartCPU()
        {
            Registers = new Dictionary<string, int>();
        }

        public SmartCPU(object instructionSet)
        {
            LoadInstructionSet(instructionSet);
        }

        public void ExecuteCurrentInstruction()
        {
            byte b = RAM.Memory[Registers[ProgramCounterKey]++];
            
            Instruction instruction = InstructionSet[b];

            ExecuteInstruction(instruction);

            throw new NotImplementedException();
        }

        private static void ExecuteInstruction(Instruction instruction)
        {
            switch (instruction.Action)
            {
                case CpuAction.ReadMemory:
                    

                    break;

                default:

                    break;
            }


            throw new NotImplementedException();
        }

        public void DefineRegisters()
        {
            throw new NotImplementedException();
        }

        public void LoadInstructionSet(object instructionSet)
        {
            throw new NotImplementedException();
        }

        //TODO temporary method to create rudimentary instruction set 
        //TODO ...during development
        public void TempCreateInstructionSet()
        {
            Registers.Add("A", 0);
            Registers.Add("B", 0);
            Registers.Add("D", 0);
            Registers.Add("X", 0);
            Registers.Add("Y", 0);
            Registers.Add("U", 0);
            Registers.Add("S", 0);

            Registers.Add("PC", 0);
            Registers.Add("CC", 0);

            ProgramCounterKey = "PC";
            ConditionCodeKey = "CC";

            InstructionSet = new Instruction[0x10ff];

            Instruction ldaImm = new Instruction();
            ldaImm.Opcode = "87";
            ldaImm.Mnemonic = "LDA";
            ldaImm.RegisterKey = "A";
            ldaImm.Write = false;
            ldaImm.Destination = "A";
            ldaImm.Bytes = 1;
            InstructionSet[0x87] = ldaImm;

        }
    }
}